<template>

  <div label="资源">
    <div class="search-box">
      <div class="search-item">
        <div class="search-label">状态:</div>
        <div class="search-content">
          <el-select v-model="permission" placeholder="请选择"  size="mini" clearable="">
            <el-option  key="0" label="待审核" value="0"></el-option>
            <el-option  key="1" label="通过" value="1"></el-option>
            <el-option  key="-1" label="未通过" value="-1"></el-option>
          </el-select>
        </div>
      </div>

      <div class="search-item">
        <el-button type="primary" size="mini" icon="el-icon-search" @click="loadObjs">查询</el-button>
        <el-button type="primary" size="mini" icon="el-icon-plus"
                   @click="showAddView">
          添加资源
        </el-button>
      </div>

    </div>



    <el-main style="padding-left: 0px;padding-top: 0px">
      <div class="table-box">
        <el-table
          :data="objs"
          v-loading="tableLoading"
          border
          stripe
          @selection-change="handleSelectionChange"
          size="mini"
          style="width: 100%">
          <el-table-column
            type="selection"
            align="left"
          >
          </el-table-column>
          <el-table-column
            prop="name"
            label="名称"
          >
          </el-table-column>



          <el-table-column
            prop="permission"
            :formatter="toPermissionLabel"
            label="状态"
          >



          </el-table-column>



          <el-table-column
            label="操作">
            <template slot-scope="scope">
              <el-button @click="showEditView(scope.row)" type="text"
                         size="mini">编辑
              </el-button>
              <el-button type="text" size="mini" v-if="scope.row.permission==0"
                         @click="deleteObj(scope.row)">删除
              </el-button>
              <el-button type="text" size="mini"
                         @click="downloadAttachment(scope.row.record_url)">查看
              </el-button>
            </template>
          </el-table-column>
        </el-table>

        <el-pagination
          background
          :page-size="10"
          :current-page="currentPage"
          @current-change="currentChange"
          layout="prev, pager, next"
          :total="totalCount">
        </el-pagination>
        <div class="form-edit-btns" style="float:right">
          <el-button type="danger" size="mini" v-if="objs.length>0" :disabled="multipleSelection.length==0"
                     @click="deleteMany">批量删除
          </el-button>
        </div>
      </div>
    </el-main>

    <el-dialog
      :title="dialogTitle"
      style="padding: 0px;"
      :close-on-click-modal="false"
      :visible.sync="dialogVisible"
      width="77%">

      <el-form :model="obj" :rules="rules" ref="addForm" size="mini" label-width="100px">


        <el-form-item label="名称:" prop="name">
          <el-input prefix-icon="el-icon-edit" v-model="obj.name" size="mini"
                    placeholder="请输入资源名称"></el-input>
        </el-form-item>



        <el-form-item label="文件:">
          <el-upload class="avatar-uploader" action="/file/upload" name = "fileData" :on-success="handleUploadSuccess"
                     :multiple="false" :show-file-list="true" :limit="1">

            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传pdf文件，且不超过10M</div>
          </el-upload>



        </el-form-item>




        <el-form-item>
          <el-button size="mini" v-if="obj.permission==0" @click="cancelEidt">取 消</el-button>
          <el-button size="mini" v-if="obj.permission==0" type="primary" @click="add('addForm')">确 定</el-button>
        </el-form-item>


      </el-form>

    </el-dialog>

  </div>




</template>
<script>
  import ElRow from "element-ui/packages/row/src/row";

  export default {
    components: {ElRow},
    data() {
      return {
        objs: [],
        dialogTitle: '',
        multipleSelection: [],
        totalCount: -1,
        currentPage: 1,
        dialogVisible: false,
        tableLoading: false,
        permission:'',
        obj: {
          name: '',
          course_id:this.$route.query.course_id,
          permission:0,
          record_url:''
        },
        rules: {
          name: [{required: true, message: '必填:请输入直播活动名称', trigger: 'blur'}],
        }
      };
    },
    mounted: function () {
      this.loadObjs();
    },
    methods: {
      downloadAttachment(url){
        location.href = url;
      },

      //时间格式化
      dateFormat:function(row, column) {
        var date = row[column.property];
        if (date == undefined) {
          return "";
        }
        return moment(date).format("YYYY-MM-DD HH:mm:ss");
      },

      toPermissionLabel:function (row) {
        if (row.permission==0) {
          return "待审核";
        } else if (row.permission==1) {
          return "通过";
        } else if (row.permission==-1) {
          return "未通过";
        }
      },
      handleUploadSuccess(result,file){
        this.obj.record_url = result.url;
      },



      handleSelectionChange(val) {
        this.multipleSelection = val;
      },
      deleteMany(){
        this.$confirm('此操作将删除[' + this.multipleSelection.length + ']条数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          var ids = '';
          for (var i = 0; i < this.multipleSelection.length; i++) {
            ids += this.multipleSelection[i].id + ",";
          }
          this.doDelete(ids);
        }).catch(() => {
        });
      },
      deleteObj(row){
        this.$confirm('此操作将永久删除[' + row.name + '], 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.doDelete(row.id);
        }).catch(() => {
        });
      },
      doDelete(ids){
        this.tableLoading = true;
        var _this = this;
        this.deleteRequest("/resource/delete/" + ids).then(resp=> {
          _this.tableLoading = false;
          if (resp && resp.status == 200) {
            var data = resp.data;
            _this.$message({type: data.status, message: data.msg});
            _this.loadObjs();
          }
        })
      },


      currentChange(currentChange){
        this.currentPage = currentChange;
        this.loadObjs();
      },
      loadObjs(){
        var _this = this;
        this.tableLoading = true;
        this.getRequest("/resource/list?page=" + this.currentPage + "&size=10&permission="+this.permission+"&course_id="+this.$route.query.course_id).then(resp=> {
          this.tableLoading = false;
          if (resp && resp.status == 200) {
            var data = resp.data;
            _this.objs = data.data.arrays;
            _this.totalCount = data.data.count;
          }
        })
      },



      add(formName){
        var _this = this;
        this.$refs[formName].validate((valid) => {
          if (valid) {
            if (this.obj.id) {
              //更新
              this.tableLoading = true;
              this.postRequest("/resource/update", this.obj).then(resp=> {
                _this.tableLoading = false;
                if (resp && resp.status == 200) {
                  var data = resp.data;
                  _this.$message({type: data.status, message: data.msg});
                  _this.dialogVisible = false;
                  _this.emptyObjData();
                  _this.loadObjs();
                }
              })
            } else {
              //添加
              this.tableLoading = true;
              this.postRequest("/resource/add", this.obj).then(resp=> {
                _this.tableLoading = false;
                if (resp && resp.status == 200) {
                  var data = resp.data;
                  _this.$message({type: data.status, message: data.msg});
                  _this.dialogVisible = false;
                  _this.emptyObjData();
                  _this.loadObjs();
                }
              })
            }
          } else {
            return false;
          }
        });
      },
      cancelEidt(){
        this.dialogVisible = false;
        this.emptyObjData();
      },
      showEditView(row){
        //console.log(row)
        this.dialogTitle = "编辑直播活动";
        this.obj.name = row.name;
        this.obj.permission = row.permission;
        this.obj.id = row.id;
        this.obj.record_url = row.record_url;
        this.dialogVisible = true;
      },



      showAddView(){
        this.dialogTitle = "添加直播活动";
        this.dialogVisible = true;
        var _this = this;
      },
      emptyObjData(){
        this.obj = {
          name: '',
          permission:0,
          course_id:this.$route.query.course_id,

        }
      }
    }
  };
</script>
<style scoped>
  .el-dialog__body {
    padding-top: 0px;
    padding-bottom: 0px;
  }

  .slide-fade-enter-active {
    transition: all .8s ease;
  }

  .slide-fade-leave-active {
    transition: all .8s cubic-bezier(1.0, 0.5, 0.8, 1.0);
  }

  .slide-fade-enter, .slide-fade-leave-to {
    transform: translateX(10px);
    opacity: 0;
  }

  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }

  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
</style>
